use IVdata_replication.dta, clear
set more off

**********************************************
sort comuna municipal
xtset comuna municipal

gen alc_map=l.alcalde_map // Mapuche mayor elected in 2004 gets into office in 2005

*** DEPENDENT VARIABLE AND CONTROLS
gen df_alcalde_map=f.d.alcalde_map
global dfcontrols f.d.mapperc f.d.pobreza f.d.urbanidad f.d.logpop f.d.log_ingreso f.d.gini 


**********************************************
*** TABLE 3
**********************************************
reg df_alcalde_map z $dfcontrols if municipal_period==1
test z
outreg2 using table3, nonotes keep(z) bracket dec(2) nocon noni replace adds(F-test, r(F))
reg df_alcalde_map z $dfcontrols if municipal_period==2
test z
outreg2 using table3, nonotes keep(z) bracket dec(2) nocon noni append adds(F-test, r(F))
reg df_alcalde_map z $dfcontrols if municipal_period==3
test z
outreg2 using table3, nonotes keep(z) bracket dec(2) nocon noni append adds(F-test, r(F))
reg df_alcalde_map z $dfcontrols if municipal_period==4
test z
outreg2 using table3, nonotes keep(z) bracket dec(2) nocon noni append adds(F-test, r(F))
reg df_alcalde_map z $dfcontrols if municipal_period==5
test z
outreg2 using table3, nonotes keep(z) bracket dec(2) nocon noni append adds(F-test, r(F))



**********************************************
use IVdata_replication.dta, clear
set more off

**********************************************
sort comuna year 
xtset comuna year

*** DEPENDENT VARIABLE
gen d_alcalde_map=d.alcalde_map
gen df_alcalde_map=f.d.alcalde_map

carryforward alcalde_map z, replace
rename z ztmp
gen alc_map=l.alcalde_map 
gen z=l.ztmp
drop ztmp

*****************************************
gen yr0=2001
gen yr1=2008 // 1) 2008 & 2)2005
gen time=0
replace time=1 if year>=yr0&year<=2004
replace time=2 if year>=2005&year<=yr1

global conflicto1 event_total event_attack event_invasion event_protest  
global controls mapperc pobreza urbanidad logpop log_ingreso gini x43
global dcontrols  d.mapperc d.pobreza d.urbanidad d.logpop d.log_ingreso d.gini 

collapse (mean) $conflicto1 $controls (mean) x23 alc_map z , by(comuna region regionsur time)

drop if time==0
xtset comuna time


*** Table 5: column (1)
reg d.event_total d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni replace
ivreg2 d.event_total (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_total l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
*** Table 5: column (3)
reg d.event_attack d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_attack (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_attack l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
*** Table 5: column (5)
reg d.event_invasion d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_invasion (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_invasion l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
*** Table 5: column (7)
reg d.event_protest d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_protest (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_protest l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append



**********************************************
use IVdata_replication.dta, clear
set more off

**********************************************
sort comuna year 
xtset comuna year

*** DEPENDENT VARIABLE
gen d_alcalde_map=d.alcalde_map
gen df_alcalde_map=f.d.alcalde_map

carryforward alcalde_map z, replace
rename z ztmp
gen alc_map=l.alcalde_map 
gen z=l.ztmp
drop ztmp

*****************************************
gen yr0=2001
gen yr1=2005 // 1) 2008 & 2)2005
gen time=0
replace time=1 if year>=yr0&year<=2004
replace time=2 if year>=2005&year<=yr1

global conflicto1 event_total event_attack event_invasion event_protest  
global controls mapperc pobreza urbanidad logpop log_ingreso gini x43
global dcontrols  d.mapperc d.pobreza d.urbanidad d.logpop d.log_ingreso d.gini 

collapse (mean) $conflicto1 $controls (mean) x23 alc_map z , by(comuna region regionsur time)

drop if time==0
xtset comuna time

*** Table 5: column (2)
reg d.event_total d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_total (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_total l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
*** Table 5: column (4)
reg d.event_attack d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_attack (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_attack l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
*** Table 5: column (6)
reg d.event_invasion d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_invasion (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_invasion l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
*** Table 5: column (8)
reg d.event_protest d.alc_map $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map) bracket dec(2) nocon noni append
ivreg2 d.event_protest (d.alc_map=l.z) $dcontrols
outreg2 using table4, nonotes  keep(d.alc_map l.z) bracket dec(2) nocon noni append
reg d.event_protest l.z $dcontrols
outreg2 using table4, nonotes  keep(l.z) bracket dec(2) nocon noni append
